import rqdatac as rq
from itertools import product
import os
import pandas as pd


raw_path = "D:\daily work\ml\\raw"


def read_raw_data():
    files = os.listdir(raw_path)
    all_train = []
    for file_name in files:
        sec_id, _ = file_name.split("_")
        df = pd.read_csv(f"{raw_path}\\{file_name}")

        df["datetime"] = pd.to_datetime(df["datetime"])
        df.set_index("datetime", inplace=True)
        all_train.append(df)

    train_df = pd.concat(all_train)
    train_df.sort_index(inplace=True)
    return train_df


def generate_parameters(parameter_settings):
    params = {}
    for para, para_setting in parameter_settings.items():
        start = para_setting["start"]
        step = para_setting["step"]
        end = para_setting["end"]
        para_list = []
        if start == end:
            para_list.append(start)
        while start < end:
            para_list.append(start)
            start += step
        params[para] = para_list

    keys = params.keys()
    values = params.values()
    products = list(product(*values))

    settings = []
    for p in products:
        setting = dict(zip(keys, p))
        settings.append(setting)

    return settings


def make_domaint_time_dict(sec_id):
    if not rq.initialized():
        rq.init("13570866213", "39314656")
    domaint_time = rq.futures.get_dominant(sec_id.upper(), start_date="20220901", end_date="20231116")
    i0_first = domaint_time.drop_duplicates(keep="first")
    i0_last = domaint_time.drop_duplicates(keep="last")
    domaint_time_dict = {}
    for k, v in i0_first.items():
        symbol_time_dict = domaint_time_dict.setdefault(v, {})
        symbol_time_dict["start"] = k

    for k, v in i0_last.items():
        symbol_time_dict = domaint_time_dict.setdefault(v, {})
        symbol_time_dict["last"] = k
    return domaint_time_dict


sec_to_wh_ind = {
    "al": "whgy",
    "cu": "whgy",
    "fu": "whgy",
    "zn": "whgy",
    "rb": "whgy",
    "pb": "whgy",
    "j": "whgy",
    "i": "whgy",
    "FG": "whgy",
    "jm": "whgy",
    "bu": "whgy",
    "hc": "whgy",
    "SM": "whgy",
    "SF": "whgy",
    "ni": "whgy",
    "sc": "whgy",
    "sp": "whgy",
    "ss": "whgy",
    "pg": "whgy",
    "lu": "whgy",
    "ao": "whgy",
    "lc": "whgy",
    "PX": "whgy",
    "sh": "whgy",

    "br": "whhg",
    "PF": "whhg",
    "SA": "whhg",
    "eb": "whhg",
    "UR": "whhg",
    "nr": "whhg",
    "eg": "whhg",
    "pp": "whhg",
    "MA": "whhg",
    "v": "whhg",
    "l": "whhg",
    "TA": "whhg",
    "ru": "whhg",
}


price_tick_map = {
    "a": 1.0,
    "ag": 1.0,
    "al": 5.0,
    "AP": 1.0,
    "au": 0.02,
    "b": 1.0,
    "bb": 0.05,
    "bc": 10.0,
    "bu": 2.0,
    "c": 1.0,
    "CF": 5.0,
    "CJ": 5.0,
    "cs": 1.0,
    "cu": 10.0,
    "CY": 5.0,
    "eb": 1.0,
    "eg": 1.0,
    "ER": 0.0,
    "fb": 0.5,
    "FG": 1.0,
    "fu": 1.0,
    "hc": 1.0,
    "i": 0.5,
    "IC": 0.2,
    "IF": 0.2,
    "IH": 0.2,
    "j": 0.5,
    "jd": 1.0,
    "jm": 0.5,
    "JR": 1.0,
    "l": 5.0,
    "lh": 5.0,
    "LR": 1.0,
    "lu": 1.0,
    "m": 1.0,
    "MA": 1.0,
    "ME": 0.0,
    "ni": 10.0,
    "nr": 5.0,
    "OI": 1.0,
    "p": 2.0,
    "pb": 5.0,
    "PF": 2.0,
    "pg": 1.0,
    "PK": 2.0,
    "PM": 1.0,
    "pp": 1.0,
    "rb": 1.0,
    "RI": 1.0,
    "RM": 1.0,
    "RO": 0.0,
    "rr": 1.0,
    "RS": 1.0,
    "ru": 5.0,
    "s": 0.0,
    "SA": 1.0,
    "sc": 0.1,
    "SF": 2.0,
    "SM": 2.0,
    "sn": 10.0,
    "sp": 2.0,
    "SR": 1.0,
    "ss": 5.0,
    "T": 0.005,
    "TA": 2.0,
    "TC": 0.0,
    "TF": 0.005,
    "TS": 0.005,
    "UR": 1.0,
    "v": 5.0,
    "WH": 1.0,
    "wr": 1.0,
    "WS": 0.0,
    "WT": 0.0,
    "y": 2.0,
    "ZC": 0.2,
    "zn": 5.0,
    "BT": 0.001,
    "ET": 0.001,
    "DO": 0.000001,
    "XR": 0.0001
}

